<template>
	<view>
		<view class="center " v-for="item in lists" :key="item.id">
			<view class="d-flex bb" @tap="">
				<view class="flex-1 mt-3 mb-3 ml-2 fs28 ls2">
					<span>{{item.changetype}}(相关：{{item.usernumber}})</span>
				</view>
				<span style="color: red;" v-if="item.recordtype == 1">+{{item.money}}</span>
				<span style="color: green;" v-else>-{{item.money}}</span>
			</view>
			<view class="d-flex bb" @tap="">
				<view class="flex-1 mt-3 mb-3 ml-2 fs28 ls2">
					<span>{{item.moneytype}}</span>
				</view>
				<span>{{item.createtime}}</span>
			</view>

		</view>

		<uni-load-more :status="loadingType"></uni-load-more>
	</view>
</template>
<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				page: 1,
				totalCount: 0,
				lists: [],
				loadingType: 'more',
				changetype: 0,
			}
		},
		onLoad(options) {
			uni.showLoading({
				title: '加载中...'
			});

			if (!!options.type) {
				this.changetype = options.type;
			}

			if (this.hasLogin) {
				this.loadData();
			}

			setTimeout(() => {
				uni.hideLoading();
			}, 600)

		},
		onPullDownRefresh() {
			this.loadingType = 'more'
			this.loadData('refresh')
		},

		//下拉到底获取新的数据
		onReachBottom() {
			if (!this.hasLogin) {
				this.islogin = true;
			} else {
				this.loadData()
			}
		},

		computed: {
			...mapState(['hasLogin']),
		},
		methods: {
			...mapMutations(['logout']),

			async loadData(type = "add") {
				if (this.loadingType == 'nomore') {
					return false;
				}
				uni.showLoading({
					title: '加载中...'
				});

				if (type == 'refresh') {
					this.page = 1
					this.lists = []
				}

				let token = uni.getStorageSync('token');
				let dataObj = {
					page: this.page,
					token: token
				}

				if (this.changetype > 0) {
					dataObj.changetype = this.changetype;
				}

				let jdata = await this.$myRequest({
					url: '/financialFlow',
					data: dataObj,
					methods: 'POST'
				})

				let ndata = jdata.data.data.data;

				this.totalCount = jdata.data.data.totalCount;

				if (this.page === 1) {
					this.lists = ndata;
					if (ndata.length === 0) {
						this.$api.msg('亲！暂无数据！！！')
					} else {
						this.page = this.page + 1;
					}
				} else {
					if (ndata.length === 0) {
						if (this.lists.length === 0) {
							this.$api.msg('亲！暂无数据！！！')
						} else {
							this.$api.msg('亲！已经到底了！！！')
						}

					} else {
						this.lists = [...this.lists, ...ndata];
						this.page = this.page + 1;
					}
				}

				if (this.totalCount > this.lists.length) {
					this.loadingType = 'more'
				} else {
					this.loadingType = 'nomore'
				}

				uni.hideLoading();
				if (type == 'refresh') {
					uni.stopPullDownRefresh()
				}
			},
		}
	}
</script>

<style>
	.btn {
		color: white;
		background-color: #29ad97;
		border-radius: 50rpx;
		margin: 100rpx 20rpx;
		font-size: 32upx;
		letter-spacing: 2upx;
	}

	page {
		background-color: #f0efee;
	}

	.center {
		width: 94%;
		height: 100%;
		background-color: white;
		margin: 20rpx auto;
		border-radius: 20rpx;
	}

	.bb {
		border: 1rpx solid #f3f3f3;
	}

	.d-flex {
		align-items: center;
	}

	.d-flex span {
		margin-right: 40upx;
	}
</style>
